const router = require('express').Router();
const userModel = require('../models/user')

//  conner 123

router.get('/login',(req,res)=>{
    
    res.render("login")
})

// 登录接口
router.post("/login",async (req,res)=>{
    console.log(req.body)
    const { userName,pwd } = req.body;
    // 判断是否有该用户，判断密码
    let user = await userModel.findOne({
        userName
    })
    if(user){
        // 有当前用户
        if( pwd == user.pwd   ){
            // 设置 cookie 存储用户登录状态
            res.cookie('userInfo',JSON.stringify({
                userName:user.userName,
                avatar:'https://himg.bdimg.com/sys/portraitn/item/d1846c697869616e676d616769638c27'
            }))
            res.send({
                code:0,
                msg:"登录成功"
            })
        }else{
            res.send({
                code:-2,
                msg:"密码错误"
            })
        }
    }else{
        res.send({
            code:-1,
            msg:"用户名不存在"
        })
    }
   
})

module.exports = router